package com.yuchen.ishop.dictionary;

import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.yuchen.core.jdbc.BaseDao;
import com.yuchen.jaxrs.ResponseUtils;
import com.yuchen.jaxrs.Responses;
import com.yuchen.utils.fastjson.JsonUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Transactional
@Path("addressService")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
public class AddressServiceImpl  implements AddressService{

    private final static Logger LOG = Logger.getLogger(AddressService.class);

    @Autowired
    private BaseDao dao;

    @GET
    @Path("find")
    @Transactional(readOnly = true)
    public List<Map> find() {
        String sql ="select code_no as id ,name as name, parent_code_no as pid from t_basic_address";
        List list = dao.querySql(sql, null);
        return list;
    }

    @GET
    @Path("findByParentId")
    @Transactional(readOnly = true)
    public Responses findByParentId(@QueryParam("parentId") String parentId) {
        Map<String, Object> params = new HashMap<String, Object>();
        if("null".equals(parentId))parentId=null;
        params.put("parentId",parentId);
        Responses responses = ResponseUtils.success();
        StringBuilder sql = new StringBuilder();
        sql.append("select code_no as id ,name as name, parent_code_no as pid from t_basic_address ");
        if(params.get("parentId") != null){
            sql.append("where parent_code_no = @parentId");
        }else{
            sql.append("where parent_code_no is null ");
        }
        List list = dao.querySql(sql.toString(), params);
        responses.setJson(JsonUtils.toJSON(list));
        return responses;
    }

}
